//
//  ViewController.m
//  Tel
//
//  Created by 宋 on 15/11/26.
//  Copyright © 2015年 宋. All rights reserved.
//

#import "ViewController.h"
#import "InputTableViewController.h"
@interface ViewController ()<InputFinshDelegate>
{
    NSMutableArray *name;
    NSMutableArray *num;
    NSIndexPath *x;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   /*name = @[
            @"汪晓",
            @"宋京科",
            ];
    num = @[
            @"15200411916",
            @"18373358860"
            ];*/
    NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
    //[def setObject:name forKey:@"name"];
    //[def setObject:num forKey:@"num"];
    //[def synchronize];
    name = [[def objectForKey:@"name"] mutableCopy];
    num = [[def objectForKey:@"num"] mutableCopy];
    //a删除
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(onEditing:)];
    
}

- (void)onEditing:(UIBarButtonItem *)btn
{
    [self.tableView setEditing:!self.editing animated:YES];
    btn.title = self.editing ? @"完成":@"编辑";
    [self.tableView reloadData];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return tableView.isEditing ? name.count + 1: name.count;
}

//显示+
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return indexPath.row - name.count ? UITableViewCellEditingStyleDelete : UITableViewCellEditingStyleInsert;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    
    if (indexPath.row == name.count) {
        return cell;
    }
    
    cell.textLabel.text = name[indexPath.row];
    cell.detailTextLabel.text = num[indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    InputTableViewController *con = [[InputTableViewController alloc] initWithStyle:UITableViewStyleGrouped];
    x = indexPath;
    
    con.tel = num[indexPath.row];
    con.delegate = self;
    [self.navigationController pushViewController:con animated:YES];
    
}

- (void)inputFinshed:(NSString *)text
{
    NSMutableArray *arr = num.mutableCopy;
    [arr replaceObjectAtIndex:x.row withObject:text];
    num = arr;
    NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
    [def setObject:num forKey:@"num"];
    [self.tableView reloadData];
}

//拖动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

//a删除

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [name removeObjectAtIndex:indexPath.row];
    [num removeObjectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
